package hu.uniobuda.nik.bsik2f;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.media.MediaMetadataRetriever;

import java.io.File;
import java.util.Arrays;

public class TagInfo {
    public String path, album, album_artist, artist, duration, genre, title,
            year;
    byte[] cover;
    final static String[] supportedExtensions = {"mp3", "ogg", "wav",
            "mp2", "mp1", "aiff", "m2a", "mpa", "m1a", "mpg", "mpeg", "aif",
            "mp3pro", "bwf", "mus", "wma", "wmv", "aac", "adts", "mp4", "m4a",
            "m4b", "mod", "mo3", "s3m", "xm", "it", "mtm", "umx", "mdz", "s3z",
            "itz", "xmz"};

    public TagInfo(String path) {
        MediaMetadataRetriever mdr = new MediaMetadataRetriever();
        mdr.setDataSource(path);
        this.path = path;
        this.album = mdr
                .extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM);
        this.album_artist = mdr
                .extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUMARTIST);
        this.artist = mdr
                .extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST);
        this.duration = mdr
                .extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
        this.genre = mdr
                .extractMetadata(MediaMetadataRetriever.METADATA_KEY_GENRE);
        this.title = mdr
                .extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE);
        this.year = mdr
                .extractMetadata(MediaMetadataRetriever.METADATA_KEY_YEAR);
        this.cover = mdr.getEmbeddedPicture();
    }

    public Bitmap getCoverArt() {

        return BitmapFactory.decodeByteArray(cover, 0, cover.length);
    }

    // SRC:
    // http://stackoverflow.com/questions/6381780/is-there-possibility-to-resize-the-image-programmatic-by-click-on-button
    public Bitmap getCoverArtThumb(int targetSize) {
        Bitmap bitMapImage = null;

        if (cover != null) {
            // First, get the dimensions of the image
            Options options = new Options();
            options.inJustDecodeBounds = true;

            try {
                BitmapFactory.decodeByteArray(cover, 0, cover.length);
            } catch (java.lang.OutOfMemoryError e) {
                // Val�sz�n�leg t�l nagy a bor�t� (2400x2400-n�l ezt dobta AVD)
                // dobjunk vissza nullt �s akkor a "nincs bor�t�" k�p ker�l a
                // hely�re
                return null;
            }
            double sampleSize = 0;
            // Only scale if we need to
            // (16384 buffer for img processing)
            Boolean scaleByHeight = Math.abs(options.outHeight - targetSize) >= Math
                    .abs(options.outWidth - targetSize);
            if (options.outHeight * options.outWidth * 2 >= 1638) {
                // Load, scaling to smallest power of 2 that'll get it <=
                // desired
                // dimensions
                sampleSize = scaleByHeight ? options.outHeight / targetSize
                        : options.outWidth / targetSize;
                sampleSize = (int) Math.pow(2d,
                        Math.floor(Math.log(sampleSize) / Math.log(2d)));
            }
            // Do the actual decoding
            options.inJustDecodeBounds = false;
            options.inTempStorage = new byte[128];
            while (true) {
                try {
                    options.inSampleSize = (int) sampleSize;
                    bitMapImage = BitmapFactory.decodeByteArray(cover, 0,
                            cover.length);

                    break;
                } catch (Exception ex) {
                    try {
                        sampleSize = sampleSize * 2;
                    } catch (Exception ex1) {

                    }
                }
            }
        }
        return bitMapImage;

    }

    public static String getExtension(File f) {
        String ext = null;
        String s = f.getName();
        int i = s.lastIndexOf('.');

        if (i > 0 && i < s.length() - 1) {
            ext = s.substring(i + 1).toLowerCase();
        }
        return ext;
    }

    public static boolean isSupported(File file) {
        String ext = getExtension(file);
        ext = ext.substring(0, ext.lastIndexOf('.'));
        return Arrays.asList(supportedExtensions).contains(ext);
    }
}
